package com.ruoyi.stock.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.HttpClientSslUtils;
import com.ruoyi.stock.domain.StockDfcf;
import com.ruoyi.stock.mapper.StockDfcfMapper;
import com.ruoyi.stock.service.IStockDfcfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 东方财富数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-08
 */
@Service
public class StockDfcfServiceImpl implements IStockDfcfService 
{
    @Autowired
    private StockDfcfMapper stockDfcfMapper;

    /**
     * 查询东方财富数据
     * 
     * @param id 东方财富数据主键
     * @return 东方财富数据
     */
    @Override
    public StockDfcf selectStockDfcfById(Long id)
    {
        return stockDfcfMapper.selectStockDfcfById(id);
    }

    /**
     * 查询东方财富数据列表
     * 
     * @param stockDfcf 东方财富数据
     * @return 东方财富数据
     */
    @Override
    public List<StockDfcf> selectStockDfcfList(StockDfcf stockDfcf)
    {
        return stockDfcfMapper.selectStockDfcfList(stockDfcf);
    }

    /**
     * 新增东方财富数据
     * 
     * @param stockDfcf 东方财富数据
     * @return 结果
     */
    @Override
    public int insertStockDfcf(StockDfcf stockDfcf)
    {
        stockDfcf.setCreateTime(DateUtils.getNowDate());
        return stockDfcfMapper.insertStockDfcf(stockDfcf);
    }

    @Override
    @Transactional
    public int insertStockDfcfAll() throws Exception {

        //删掉之前的
        stockDfcfMapper.deleteStockDfcfAll();
        String url="http://38.push2.eastmoney.com/api/qt/clist/get?pn=1&pz=1000&po=1&np=1&fltt=2&invt=2&dect=1&fid=f3&fs=m:1+t:2,m:1+t:23";
        //
        url="https://68.push2.eastmoney.com/api/qt/clist/get?pn=1&pz=5000&po=1&np=1&fltt=2&invt=2&dect=1&&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048&_=1737013951130";
        //"https://68.push2.eastmoney.com/api/qt/clist/get?cb=jQuery1124033968991350728817_1737013950744&pn=1&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&dect=1&wbp2u=|0|0|0|web&fid=f3&fs=m:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048&_=1737013951130";
        String[] url1=url.split("&");
        JSONObject wxJson = JSON.parseObject(HttpClientSslUtils.doGet(url));
        JSONObject  obj=   JSON.parseObject(wxJson.get("data").toString());
        JSONArray obj1=  obj.getJSONArray("diff");
        StockDfcf stockDfcf=new StockDfcf();
        for(int i=0;i<obj1.size();i++){
            stockDfcf.setSecurityName(JSON.parseObject(obj1.get(i).toString()).get("f14").toString());
            stockDfcf.setSecurityCode(JSON.parseObject(obj1.get(i).toString()).get("f12").toString());
            stockDfcf.setLastPrice((JSON.parseObject(obj1.get(i).toString()).get("f2").toString()));
            stockDfcf.setLatestIncrease((JSON.parseObject(obj1.get(i).toString()).get("f3").toString()));
            stockDfcf.setLatestChangedMoney((JSON.parseObject(obj1.get(i).toString()).get("f4").toString()));
            //  stockDfcf.setTurnover();
            //成交量，成交额
            stockDfcf.setAmplitude((JSON.parseObject(obj1.get(i).toString()).get("f7").toString()));
            stockDfcf.setHighest((JSON.parseObject(obj1.get(i).toString()).get("f15").toString()));
            stockDfcf.setHighest((JSON.parseObject(obj1.get(i).toString()).get("f15").toString()));
            stockDfcf.setLowest((JSON.parseObject(obj1.get(i).toString()).get("f16").toString()));
            stockDfcf.setTodayOpen((JSON.parseObject(obj1.get(i).toString()).get("f17").toString()));
            stockDfcf.setYesterdayClose((JSON.parseObject(obj1.get(i).toString()).get("f18").toString()));
            stockDfcfMapper.insertStockDfcf(stockDfcf);
        }
        return obj1.size();
    }

    /**
     * 修改东方财富数据
     * 
     * @param stockDfcf 东方财富数据
     * @return 结果
     */
    @Override
    public int updateStockDfcf(StockDfcf stockDfcf)
    {
        stockDfcf.setUpdateTime(DateUtils.getNowDate());
        return stockDfcfMapper.updateStockDfcf(stockDfcf);
    }

    /**
     * 批量删除东方财富数据
     * 
     * @param ids 需要删除的东方财富数据主键
     * @return 结果
     */
    @Override
    public int deleteStockDfcfByIds(Long[] ids)
    {
        return stockDfcfMapper.deleteStockDfcfByIds(ids);
    }

    /**
     * 删除东方财富数据信息
     * 
     * @param id 东方财富数据主键
     * @return 结果
     */
    @Override
    public int deleteStockDfcfById(Long id)
    {
        return stockDfcfMapper.deleteStockDfcfById(id);
    }
}
